﻿///////////////////////////////////////////////////////////////////////////////
//
//  createObjects.js   			version 1.0
//
//  This file is provided by Microsoft as a helper file for websites that
//  incorporate Silverlight Objects. This file is provided under the Silverlight 
//  SDK 1.0 license available at http://go.microsoft.com/fwlink/?linkid=94240.  
//  You may not use or distribute this file or the code in this file except as 
//  expressly permitted under that license.
// 
//  Copyright (c) 2007 Microsoft Corporation. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////var bghost;
var host;
var Holder;
var bgHolder;
var menuX = 0;
var menuLength;
var currentNode = 1;
var btnArray = new Array();

function createSLObjects()
{
    //instantiates Silverlight media player in StartPlayer.js
    var player = new StartPlayer_0();
    //Create menu silverlight
    Silverlight.createObjectEx( {   source: 'XAML/menu.xaml', 
                                        parentElement: document.getElementById("menuDiv"), 
                                        id: 'Menu', 
                                        properties:{ width:'640px', height:'30px', version:'1.0', background:document.body.style.backgroundColor, isWindowless:'true' }, 
                                        events:{ onLoad:createMenu } } );
}


function createMenu(sender, eventArgs)
{
    
    //set Variables
    host = sender;
    Holder = host.content.findName("menuHolder");
    
    //set functions
    this.importXML = importXML;
    this.initMenu = initMenu;
    this.loadBTN = loadBTN;
    this.createBTN = createBTN;
    
    
    this.importXML("xml/linksXML.xml");
}

//import the xml menu information for use in the buttons
function importXML(myXML)
{		
    
    if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = Function.createDelegate(this, initMenu);
	}
	else if (window.ActiveXObject)
	{
	    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	    xmlDoc.onreadystatechange = function () 
		{
			if (xmlDoc.readyState == 4) initMenu()
		};
 	}
	else
	{
	    alert('Your browser can\'t handle this script');
		return;
	}
	
	xmlDoc.load(myXML);
}

//init btn for each menu item in xml
function initMenu()
{  
   menuLength = nodeNumber(xmlDoc);
   loadBTN()   
}


function loadBTN()
{
    if(this.currentNode <= this.menuLength)
    {   
        var myNode = sendNodes("button"+currentNode, xmlDoc);
        var myBTN = createBTN(menuX, myNode.attributes[0].nodeValue, currentNode);
        var xmlFragment = host.content.createFromXaml(myBTN);
        Holder.children.add(xmlFragment);
        var theBTN = host.content.findName("BTN"+currentNode);
        var myObject = {name: theBTN, id: currentNode, link: myNode.attributes[1].nodeValue, control:host, mama:this}
        myObject.init = setBTN;
        btnArray.push(myObject);
        myObject.init();
    }
    else
    {
        Holder["Canvas.Left"] = Math.abs(menuX-640)/2;
    }
}

function createBTN(xOffset, label, i)
{   
   
    var myXAML = '<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" RenderTransformOrigin="0.5,0.5" x:Name="BTN'+i+'" Width="56" Height="20" Canvas.Left="'+xOffset+'" Canvas.Top="5"> ';
    myXAML += '<Canvas.Resources> 	<Storyboard x:Name="Highlightover'+i+'">		<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label'+i+'" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)">			<SplineColorKeyFrame KeyTime="00:00:00.2" Value="#FF036AA3"/>		</ColorAnimationUsingKeyFrames>';
		myXAML += '<DoubleAnimation               Storyboard.TargetName="BTN'+i+'"               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"               To="1.3" Duration="0:0:00.2" />';
		myXAML += '<DoubleAnimation               Storyboard.TargetName="BTN'+i+'"               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"               To="1.3" Duration="0:0:00.2" />';
myXAML += '</Storyboard>';
		myXAML += '<Storyboard x:Name="Highlightout'+i+'">';
			myXAML += '<DoubleAnimation               Storyboard.TargetName="BTN'+i+'"               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"               To="1" Duration="0:0:00.2" />';
		myXAML += '<DoubleAnimation               Storyboard.TargetName="BTN'+i+'"               Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"               To="1" Duration="0:0:00.2" />';
			myXAML += '<ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="label'+i+'" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)">';
					myXAML += '	<SplineColorKeyFrame KeyTime="00:00:00.2" Value="#FF58A8D4"/>';
					myXAML += '</ColorAnimationUsingKeyFrames>';
		myXAML += '</Storyboard>';
	myXAML += '</Canvas.Resources>';
		myXAML += '<Canvas.RenderTransform>';
			myXAML += '<TransformGroup>';
				myXAML += '<ScaleTransform ScaleX="1" ScaleY="1"/>';
				myXAML += '<SkewTransform AngleX="0" AngleY="0"/>';
				myXAML += '<RotateTransform Angle="0"/>';
				myXAML += '<TranslateTransform X="0" Y="0"/>';
			myXAML += '</TransformGroup>';
		myXAML += '</Canvas.RenderTransform>';
myXAML += '<TextBlock x:Name="label'+i+'" Width="93.334" Height="16.333" Canvas.Left="6.666" Canvas.Top="2" FontFamily="Verdana" FontSize="11"  Foreground="#FF54B0E2" Text="'+label+'" TextWrapping="NoWrap"/><Rectangle x:Name="Background'+i+'" Width="100" Height="20" Fill="#00FFFFFF"/></Canvas>';

    return myXAML;
}

function setBTN()
{
    
    this.highlightover = this.control.content.findName("Highlightover"+this.id);
    this.highlightout = this.control.content.findName("Highlightout"+this.id);
    var background = this.control.content.findName("Background"+this.id);
    var myLabel = this.control.content.findName("label"+this.id);
    var btnWidth = myLabel["Canvas.Left"]*2 + myLabel.ActualWidth;
    background.width = btnWidth;
    this.mama.menuX += btnWidth;
    this.mama.currentNode++;
    this.mama.loadBTN();
    
    this.name.cursor = "Hand";
    var ob = Function.createDelegate(this, onButton);
    var fb = Function.createDelegate(this, offButton);
    var pb = Function.createDelegate(this, pressButton);
    this.name.addEventListener("mouseEnter", ob); 
    this.name.addEventListener("mouseLeave", fb);
    this.name.addEventListener("mouseLeftButtonDown", pb);  

}

function onButton(sender, eventArgs)
{
    this.highlightover.begin();
}

function offButton(sender, eventArgs)
{
    this.highlightout.begin();
}

function pressButton(sender, eventArgs)
{
window.open(this.link, "_blank")
}